<?php
/**
 * Created by PhpStorm.
 * User: W
 * Date: 2018/12/3
 * Time: 15:36
 */

namespace app\sports_admin\controller;
use app\sports_admin\model\CustomerSchool as CustomerSchoolModel;
use app\sports_admin\model\Region;
use think\Request;
use app\sports_admin\service\CustomerSchool as CustomerSchoolService;
use app\sports_admin\model\Train;
use app\exception\CustomerSchoolException;

class CustomerSchool extends Base
{
    protected $beforeActionList = [
        'isBusiness' => ['only'=>'index,delcustomerschool,startcustomerschool,selcustomerschool,addcustomerschool,addcustomertrain'],
        'operationSelect'=> ['only'=>'index,schoolservice,selcustomerschool'],
        'operationInsert'=> ['only'=>'addcustomerschool,addcustomertrain'],
        'operationUpdate'=> ['only'=>'startcustomerschool,delcustomerschool']
    ];
    //学校业务首页
    public function index()
    {
        //地区列表
        $place  = Region::getCity();

        return view("list",compact("place"));
    }
    //全部的学校的业务信息
    public function schoolService()
    {
        //学校列表
        $params = Request::instance()->only(['page','rows','sort','sortOrder','searchName','type']);
        $page = intval($params['page']) ? intval($params['page']) : 1;
        $rows = intval($params['rows']) ? intval($params['rows']) : 15;
        $sort = trim($params['sort']) ? trim($params['sort']) : 'id';
        $sortOrder = trim($params['sortOrder']) ? trim($params['sortOrder']) : 'asc';
        $searchName = trim($params['searchName']);
        $result = CustomerSchoolModel::getSchoolService($page,$rows,$sort,$sortOrder,$searchName);
        return json($result);
    }
    //终止合作
    public function delCustomerSchool()
    {
        $message = Request::instance()->only('id');
        $id      = $message['id'];
        $result = CustomerSchoolModel::where("id",$id)->delete();
        if($result){
            return json(['code'=>201,'msg'=>'已经终止合作']);
        }else{
            throw new CustomerSchoolException('服务器内部错误');
        }
    }

    //开始合作
    public function startCustomerSchool()
    {
        $message = Request::instance()->only('id');
        $id      = $message['id'];
        //查询学校名 学校地址  代理人id  代理人姓名 然后统一返回给添加学校页面
        $data    = CustomerSchoolService::customerSchoolMessage($id);
        //地区也返回页面
        if($data){
            return view('addSchool',compact('data'));
        }else{
            throw new CustomerSchoolException('服务器内部错误');
        }
    }

    //查询学校业务的备注信息
    public function selCustomerSchool()
    {
        $message = Request::instance()->only('id');
        $id      = $message['id'];
        $data    = CustomerSchoolService::selCustomerSchool($id);
        $msg     = $data['remark'];
        if($msg){
            return json(['code'=>201,'msg'=>"$msg"]);
        }else{
            throw new CustomerSchoolException('服务器内部错误');
        }
    }

    public function addCustomerSchool(Request $request)
    {
        if($request->isAjax()){
            return CustomerSchoolService::addCustomerSchool();
        }
    }
    public function addCustomerTrain(Request $request)
    {

        if($request->isAjax()){
            return Train::addCustomerTrain();
        }
    }



}